//
//  TodayShowView.swift
//  movieInfo
//
//  Created by zhangkai on 2023/5/22.
//

import UIKit
import StatusAlert

class TodayShowView: UIView {
    
    
    private var savelist: [Movie] = []
    
    var allModel:[Movie]?
    
    var img = MIImageView(frame: CGRect(x: 0, y: 0, width: 120, height: 180))
    var nameLabel:UILabel!
    var stattImg = UIImageView()
    var stattLabel = UILabel()
    var infoLabel = UILabel()
    
    
    var model:Movie?{
        didSet{
            guard let tempModel = model else { return }
            
            
            
            if let path = tempModel.posterPath {
                self.img.downloadImage(fromPath: path)
            }
            self.nameLabel.text = tempModel.title ?? ""
            self.stattLabel.text = "\(tempModel.voteAverage ?? 0.0)"
            self.infoLabel.text = tempModel.overview ?? ""
            
            
            
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        
        
        if let data = UserDefaults.standard.value(forKey: "savefvolist") as? Data {
            let copy = try? PropertyListDecoder().decode([Movie].self, from: data)
            savelist = copy!
        }
        
        
        
        
        let bgView = UIView()
        addSubview(bgView)
        bgView.snp.makeConstraints { make in
            make.width.equalTo(UIScreen.main.bounds.width - 40)
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview()
            make.height.equalTo(300)
        }
        bgView.backgroundColor = .white
        bgView.layer.cornerRadius = 12
        bgView.layer.masksToBounds = true
        
        bgView.addSubview(img)
        img.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(10)
            make.top.equalToSuperview().offset(20)
            make.width.equalTo(120)
            make.height.equalTo(180)
        }
        img.layer.masksToBounds = true
        img.contentMode = .scaleAspectFill
        
        nameLabel = UILabel()
        nameLabel.text = "N/AN/AN/AN/A"
        nameLabel.numberOfLines = 2
        nameLabel.textColor = .black
        nameLabel.font = UIFont.boldSystemFont(ofSize: 16)
        bgView.addSubview(nameLabel)
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(img.snp.right).offset(8)
            make.right.equalToSuperview().offset(-8)
            make.top.equalTo(img.snp.top).offset(18)
        }
        
        stattImg.image = UIImage(named: "xingxing")
        bgView.addSubview(stattImg)
        stattImg.snp.makeConstraints { make in
            make.left.equalTo(img.snp.right).offset(8)
            make.top.equalTo(nameLabel.snp.bottom).offset(15)
            make.width.equalTo(15)
            make.height.equalTo(15)
        }

        stattLabel.text = "N/A"
        stattLabel.textColor = .darkText
        stattLabel.font = UIFont.boldSystemFont(ofSize: 12)
        bgView.addSubview(stattLabel)
        stattLabel.snp.makeConstraints { make in
            make.centerY.equalTo(stattImg)
            make.left.equalTo(stattImg.snp.right)

        }


        infoLabel.text = "N/A"
        infoLabel.numberOfLines = 4
        infoLabel.textColor = .black
        infoLabel.font = UIFont.boldSystemFont(ofSize: 16)
        bgView.addSubview(infoLabel)
        infoLabel.snp.makeConstraints { make in
            make.left.equalTo(img.snp.right).offset(8)
            make.right.equalToSuperview().offset(-8)
            make.top.equalTo(stattLabel.snp.bottom).offset(8)
        }
        
        
        let btn = UIButton()
        btn.setTitle("Add Collect", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: .bold)
        btn.backgroundColor = UIColor(red: 0.17, green: 0.78, blue: 0.53, alpha: 1)
        bgView.addSubview(btn)
        btn.snp.makeConstraints { make in
            make.height.equalTo(44)
            make.left.equalToSuperview().offset(20)
            make.right.equalToSuperview().offset(-20)
            make.top.equalTo(img.snp.bottom).offset(20)
        }
        btn.layer.cornerRadius = 22
        btn.layer.masksToBounds = true
        
        btn.addTarget(self, action: #selector(clickAdd), for: .touchDown)
        
        
        
        
        let reloadbtn = UIButton()
        reloadbtn.setImage(UIImage(named: "shuaxin"), for: .normal)
        bgView.addSubview(reloadbtn)
        reloadbtn.snp.makeConstraints { make in
            make.width.height.equalTo(20)
            make.top.equalTo(img).offset(-10)
            make.right.equalToSuperview().offset(-10)
        }
        reloadbtn.addTarget(self, action: #selector(clickReload), for: .touchDown)
//
        
    }
    
    
    @objc func clickReload(){
        
        
        let model1 = allModel?.randomElement()
        self.model = model1
        
    }
    
    
    @objc func clickAdd(){
        
        
        if model != nil{
            
            
            if !self.savelist.contains(where: {$0.id == model!.id}) {
                
                let time = self.formatter.string(from: Date())
                model!.saveTime = time
                
                self.savelist.append(model!)
                UserDefaults.standard.set(try? PropertyListEncoder().encode(self.savelist), forKey: "savefvolist")
                
                
                let statusAlert = StatusAlert()
                statusAlert.image = UIImage(named: "")
                statusAlert.title = ""
                statusAlert.message = "Has been added to the collection"
                statusAlert.showInKeyWindow()
            }else{
                
                
                let statusAlert = StatusAlert()
                statusAlert.image = UIImage(named: "")
                statusAlert.title = ""
                statusAlert.message = "Has been added to the collection"
                statusAlert.showInKeyWindow()
                
            }
            
            
            
        }
        
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    fileprivate let formatter: DateFormatter = {
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy/MM/dd"
        return formatter
    }()

}
